package com.edol.painting.main;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.signature.StringSignature;
import com.edol.painting.R;
import com.edol.painting.data.PaintingDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/4.
 */

public class PaintPageAdapter extends PagerAdapter implements View.OnClickListener {
    private List<PaintingDatabase.PaintingEntry> mData;
    private List<ImageView> mViewPool = new ArrayList<ImageView>();

    private Context mContext;

    private int mChildCount = 0;
    private int mMaxThumbSize;

    public PaintPageAdapter(Context context) {
        mContext = context;
        init(context);
    }

    public PaintPageAdapter(Context context, List<PaintingDatabase.PaintingEntry> data) {
        this(context);
        mData = data;
    }

    private void init(Context context) {
        int padding = 5; //dp
        int w = context.getResources().getDisplayMetrics().widthPixels;
        mMaxThumbSize = w - padding;
    }

    public void setData(List<PaintingDatabase.PaintingEntry> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void removePaint(PaintingDatabase.PaintingEntry paintingEntry) {
        if (paintingEntry != null && mData != null) {
            mData.remove(paintingEntry);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView iv;
        if (mViewPool.size() > 0) {
            iv = mViewPool.remove(0);
        } else {
            iv = (ImageView) LayoutInflater.from(mContext).inflate(R.layout.layout_paint_page_img, null);
        }

        PaintingDatabase.PaintingEntry entry = mData.get(position);
        iv.setTag(R.id.paint_page_id, entry);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        container.addView(iv, params);

        iv.setOnClickListener(this);
        Glide.with(mContext)
                .load(entry.mThumbnail)
                .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
                .into(iv);

        return iv;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof ImageView) {
            container.removeView((ImageView)object);
            ((ImageView) object).setImageDrawable(null);
            ((ImageView) object).setTag(R.id.paint_page_id, null);
        }
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
    }


    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.paint_page_id);
        if (tag instanceof PaintingDatabase.PaintingEntry) {
            PaintingDatabase.PaintingEntry entry = (PaintingDatabase.PaintingEntry) tag;
            PaintController.getInstances().enterBlankPaint(mContext, entry.mId, entry.mTheme);
        }
    }

    @Override
    public void notifyDataSetChanged() {
        mChildCount = getCount();
        super.notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        if (mChildCount > 0) {
            mChildCount--;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }
}
